package com.example.webdownloadserver.core;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;


/**
 * 
 * <p>
 * 装载上传下载服务业务类
 * </p>
 * @title TaskHandlerInit.java
 * @package com.dnkj.download.task.core 
 * @author ying.xiao
 * @version 0.1 2020年12月5日
 */
@Component
public class TaskHandlerInit implements ApplicationListener<ContextRefreshedEvent> {

	private Logger logger = LoggerFactory.getLogger(TaskHandlerInit.class);
	
	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		try {
			logger.debug(">>>>>>>>>>init download handler map begin ");
			Map<String, Object> downloanHandlerMap = TaskHandlerUtil.getMapbeanwithAnnotion(DownloadHandler.class);
			for (Map.Entry<String, Object> entrymap : downloanHandlerMap.entrySet()) {
				try {
					Object object = TaskHandlerUtil.getTarget(entrymap.getValue());
					if (object != null) {
						ITaskProcess taskProcess = (ITaskProcess) object;
						DownloadHandler info = taskProcess.getClass().getAnnotation(DownloadHandler.class);
						String handler = info.handler();
						if (StringUtils.isEmpty(handler)) {
							handler = getDefaultHandler(object.getClass().getSimpleName());
						}
						TaskHandlerUtil.downloadHandlerBeanMap.put(handler, object);
					}
				} catch (Exception e) {
					logger.error(">>>>>>>>>>init download handler map error:", e);
				}
			}
			logger.debug(">>>>>>>>>>init download handler map success ");
		} catch (Exception e) {
			logger.error("TaskHandlerInit ERROR",e);
		}
		
		try {
			logger.debug(">>>>>>>>>>init upload handler map begin ");
			Map<String, Object> uploadHandlerMap = TaskHandlerUtil.getMapbeanwithAnnotion(UploadHandler.class);
			for (Map.Entry<String, Object> entrymap : uploadHandlerMap.entrySet()) {
				try {
					Object object = TaskHandlerUtil.getTarget(entrymap.getValue());
					if (object != null) {
						ITaskProcess taskProcess = (ITaskProcess) object;
						UploadHandler info = taskProcess.getClass().getAnnotation(UploadHandler.class);
						String handler = info.handler();
						if (StringUtils.isEmpty(handler)) {
							handler = getDefaultHandler(object.getClass().getSimpleName());
						}
						TaskHandlerUtil.uploadHandlerBeanMap.put(handler, object);
					}
				} catch (Exception e) {
					logger.error(">>>>>>>>>>init upload handler map error:", e);
				}
			}
			logger.debug(">>>>>>>>>>init upload handler map success ");
		} catch (Exception e) {
			logger.error("TaskHandlerInit ERROR",e);
		}			
		
	}
		

	private String getDefaultHandler(String simpleClassName) {
		if (StringUtils.isEmpty(simpleClassName)) {
			return TaskHandlerConfig.EMPTY;
		}
		simpleClassName = simpleClassName.replace(TaskHandlerConfig.HANDLER_SUFFIX, TaskHandlerConfig.EMPTY);
		StringBuilder sb = new StringBuilder();
		sb.append(simpleClassName);
		StringBuilder handler = new StringBuilder();
		int j = 0;
		for (int i = 0; i < sb.length(); i++) {
			char c = sb.charAt(i);
			if (j < 2 && Character.isUpperCase(c)) {
				handler.append(TaskHandlerConfig.SEPARTOR).append(Character.toLowerCase(c));
				j++;
			} else {
				handler.append(c);
			}
		}
		return handler.toString().substring(1);
	}

}
